package Controllers;

import Common.Helper;
import Models.ChessPieceModel;
import Models.Player;
import Models.Point2D;
import Models.StandardChessPieceModel;

import java.rmi.RemoteException;

public class KnightController implements ChessPieceControllerInterface {

	@Override
	public boolean legalMove(Point2D moveVector, ChessPieceModel chessPiece) throws RemoteException {
		if (moveVector.getX()==2 && (moveVector.getY() == 1 || moveVector.getY() == -1)){
			return true;
		}
		else if (moveVector.getX()==1 && (moveVector.getY() == -2 || moveVector.getY() == 2)) {
			return true;
		}
		else if (moveVector.getX()==-1 && (moveVector.getY() == -2 || moveVector.getY() == 2)) {
			return true;
		}
		else if (moveVector.getX()==-2 && (moveVector.getY() == -1 || moveVector.getY() == 1)) {
			return true;
		}
		// TODO Auto-generated method stub
		else {
			return false;
		}
	}




	@Override
	public boolean legalCapture(Point2D moveVector, ChessPieceModel chessPiece) throws RemoteException {
		// TODO Auto-generated method stub
		return legalMove(moveVector, chessPiece);
	}



	@Override
	public boolean canPieceMoveNotify(Point2D from, Point2D to,
			ChessPieceModel chessPiece) throws RemoteException {
		return legalMove(Helper.calculateVector(from, to), chessPiece);
		}
		
		
		public static ChessPieceModel getNew(Player player) throws RemoteException {
			ChessPieceModel knight = new StandardChessPieceModel(player);
			ChessPieceControllerInterface knightController = new KnightController();
			knight.addCanPieceMoveListener(knightController);
			knight.addCanPieceCaptureListener(knightController);
			return knight;
		}




		@Override
		public boolean CanPieceCaptureNotify(Point2D from, Point2D to,
				ChessPieceModel chessPiece) throws RemoteException {
			
			return legalCapture(Helper.calculateVector(from, to), chessPiece);
		}

}
